//
//  OMTNoAlbumPermissionView.m
//  OMTImagePicker
//
//  Created by yizhaorong on 2017/11/16.
//

#import "OMTNoAlbumPermissionView.h"

#import "OMTImagePickerController.h"

@interface OMTNoAlbumPermissionView ()
// 标题
@property (nonatomic, strong) UILabel *titleLabel;
// 描述
@property (nonatomic, strong) UILabel *descriptionLabel;
// 步骤
@property (nonatomic, strong) UILabel *stepLabel;
// 允许访问按钮
@property (nonatomic, strong) UIButton *allowAccessButton;

@end

@implementation OMTNoAlbumPermissionView

#pragma mark - Lifecycle
- (void)dealloc {
    // [self unregisterNotifications]
}

- (instancetype)initWithAlbumManager:(OMTAlbumManager *)albumManager {
    if (self = [super init]) {
        _albumManager = albumManager;
        [self setupSubviews];
        [self setLayout];
    }
    return self;
}

#pragma mark - View Lifecycle
- (void)setupSubviews {
    // 初始化子视图
    NSString *(^Album18N)(NSString *) = [self.albumManager.builder.localizedString copy];
    _titleLabel = [UILabel new];
    _titleLabel.font = [UIFont boldSystemFontOfSize:16];
    _titleLabel.textColor = [UIColor colorWithWhite:0 alpha:0.7];
    _titleLabel.numberOfLines = 0;
    _titleLabel.text = Album18N(@"omt_album_allow_access_title");
    [self addSubview:_titleLabel];
    
    _descriptionLabel = [UILabel new];
    _descriptionLabel.font = [UIFont systemFontOfSize:14];
    _descriptionLabel.numberOfLines = 0;
    _descriptionLabel.textColor = [UIColor colorWithWhite:0 alpha:0.6];
    _descriptionLabel.text = Album18N(@"omt_album_allow_access_description");
    [self addSubview:_descriptionLabel];
    
    _stepLabel = [UILabel new];
    _stepLabel.font = [UIFont systemFontOfSize:13];
    _stepLabel.textColor = [UIColor colorWithWhite:0 alpha:0.7];
    _stepLabel.numberOfLines = 0;
//    _stepLabel.text = Album18N(@"omt_album_allow_access_step");
    _stepLabel.hidden = YES;
    [self addSubview:_stepLabel];
    
    _allowAccessButton = [UIButton new];
    _allowAccessButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [_allowAccessButton setTitleColor:OMTAlbumRGB(0x38d5be) forState:UIControlStateNormal];
    [_allowAccessButton setTitle:Album18N(@"omt_album_allow_access_button_title") forState:UIControlStateNormal];
    [_allowAccessButton addTarget:self action:@selector(allowAccessAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_allowAccessButton];
}

- (void)setLayout {
    // 设置或更新布局
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    CGFloat offsetY = 100;
    
    CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeMake(width, height)];
    CGFloat titleW = titleSize.width;
    CGFloat titleH = titleSize.height;
    CGFloat titleX = (width - titleW) / 2;
    CGFloat titleY = (height - titleH) / 2 - offsetY;
    CGRect titleFrame = CGRectMake(titleX, titleY, titleW, titleH);
    self.titleLabel.frame = titleFrame;
    
    CGSize descriptionSize = [self.descriptionLabel sizeThatFits:CGSizeMake(width-32, height)];
    CGFloat descriptionW = descriptionSize.width;
    CGFloat descriptionH = descriptionSize.height;
    CGFloat descriptionX = (width - descriptionW) / 2;
    CGFloat descriptionY = CGRectGetMaxY(titleFrame) + 10;
    CGRect descriptionFrame = CGRectMake(descriptionX, descriptionY, descriptionW, descriptionH);
    self.descriptionLabel.frame = descriptionFrame;
    
    CGSize stepSize = [self.stepLabel sizeThatFits:CGSizeMake(width, height)];
    CGFloat stepW = stepSize.width;
    CGFloat stepH = stepSize.height;
    CGFloat stepX = (width - stepW) / 2;
    CGFloat stepY = CGRectGetMaxY(descriptionFrame);
    if (stepH > 0) {
        stepY += 50;
    }
    CGRect stepFrame = CGRectMake(stepX, stepY, stepW, stepH);
    self.stepLabel.frame = stepFrame;
    
    CGSize allowAccessSize = [self.allowAccessButton sizeThatFits:CGSizeMake(width, height)];
    CGFloat allowAccessW = allowAccessSize.width;
    CGFloat allowAccessH = allowAccessSize.height;
    CGFloat allowAccessX = (width - allowAccessW) / 2;
    CGFloat allowAccessY = CGRectGetMaxY(stepFrame) + 64;
    CGRect allowAccessFrame = CGRectMake(allowAccessX, allowAccessY, allowAccessW, allowAccessH);
    self.allowAccessButton.frame = allowAccessFrame;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self setLayout];
}

#pragma mark - Notifications

#pragma mark - Actions
- (void)allowAccessAction:(UIButton *)sender {
    [self.delegate noAlbumPermissionViewDidClickAllowAccess:self];
}

#pragma mark - Private

#pragma mark - Public


#pragma mark - Protocol conformance


#pragma mark - Lazyload


#pragma mark - Custom Accessors
//自定义 setter getter

@end
